package com.smh.a06zipStreamDemo1;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

public class ZipStreamDemo1 {
    public static void main(String[] args) throws IOException {
        //要解压的压缩包
        File src = new File("day27-code\\file\\demo4\\myio.zip");
        //要解压的目的地
        File dest = new File("day27-code\\file\\demo4");
        unzip(src, dest);

    }

    public static void unzip(File src, File dest) throws IOException {
        ZipInputStream zis = new ZipInputStream(new FileInputStream(src));

        //获取压缩包里面的每一个zipentry对象
//        ZipEntry entry = zis.getNextEntry();
//        ZipEntry entry2 = zis.getNextEntry();
//        ZipEntry entry3 = zis.getNextEntry();
//
//        System.out.println(entry);
//        System.out.println(entry2);
//        System.out.println(entry3);
        ZipEntry entry;
        while ((entry = zis.getNextEntry()) != null) {
            if (entry.isDirectory()) {
                new File(dest.getPath(), entry.toString()).mkdirs();
            } else {
                FileOutputStream fos = new FileOutputStream(new File(dest.getPath(), entry.toString()));
                byte[] bytes = new byte[1024 * 1024];
                int len;
                while ((len = zis.read(bytes)) != -1) {
                    fos.write(bytes, 0, len);
                }
                fos.close();
                //表示压缩包里面的文件处理完了
                zis.closeEntry();
            }
            System.out.println(entry);
        }
        zis.close();
    }
}
